using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;


using Material.Object;
using Material.DAL;
namespace Material.Entity
{
	public class TblUserEntity
	{
		private Accessor accessor;
		private string connString;

		public TblUserEntity (string _connString)
		{
			 this.connString = _connString;
		}

		public int Insert(TblUser tbluser)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblUser_Insert";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add("@UserID" ,tbluser.UserID );
			 accessor.Add("@Password" ,tbluser.Password );
			 accessor.Add("@Nama" ,tbluser.Nama );
			 accessor.Add("@TipeUser" ,tbluser.TipeUser );
			 accessor.Add("@CreatedDate" ,tbluser.CreatedDate );
			 accessor.Add("@CreatedBy" ,tbluser.CreatedBy );
			 accessor.Add("@ModifiedDate" ,tbluser.ModifiedDate );
			 accessor.Add("@ModifiedBy" ,tbluser.ModifiedBy );

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public int Update(TblUser tbluser)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblUser_Update";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"UserID" ,tbluser.UserID );
			 accessor.Add(@"Password" ,tbluser.Password );
			 accessor.Add(@"Nama" ,tbluser.Nama );
			 accessor.Add(@"TipeUser" ,tbluser.TipeUser );
			 accessor.Add(@"CreatedDate" ,tbluser.CreatedDate );
			 accessor.Add(@"CreatedBy" ,tbluser.CreatedBy );
			 accessor.Add(@"ModifiedDate" ,tbluser.ModifiedDate );
			 accessor.Add(@"ModifiedBy" ,tbluser.ModifiedBy );

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public int Delete(TblUser tbluser)
		{
			 int result = -1;
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblUser_Delete";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"UserID" ,tbluser.UserID);

			 result = Excutor.ExecuteNonQuery(accessor);
			 if (result != -1)
				 accessor.CommitTransaction();
			 else
				 accessor.RollBackTransaction();
			 return result;
		}

		public TblUser GetDataByID(TblUser tbluser)
		{
			 TblUser result = new TblUser();
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblUser_SelectByID";
			 accessor.CommandType = CommandType.StoredProcedure;
			 accessor.Add(@"UserID" ,tbluser.UserID);

			 SqlDataReader rdr = Excutor.GetDataReader(accessor);
			 if (rdr.HasRows)
			 {
				 while (rdr.Read())
					 {
						 result.UserID = rdr.GetString(0);
						 result.Password = rdr.GetString(1);
						 result.Nama = rdr.GetString(2);
						 result.TipeUser = rdr.GetInt16(3);
						 result.CreatedDate = rdr.GetDateTime(4);
						 result.CreatedBy = rdr.GetString(5);
						 result.ModifiedDate = rdr.GetDateTime(6);
						 result.ModifiedBy = rdr.GetString(7);
					  }
			}
			 return result;
		}

		public List<TblUser> GetDataAll(TblUser tbluser)
		{
			 List<TblUser> result = new List<TblUser>();
			 accessor = new Accessor(true, connString);
			 accessor.CommandText = "TblUser_SelectAll";
			 accessor.CommandType = CommandType.StoredProcedure;

			 SqlDataReader rdr = Excutor.GetDataReader(accessor);
			 if (rdr.HasRows)
			 {
				 while (rdr.Read())
					 {
						 TblUser obj = new TblUser();
						 obj.UserID = rdr.GetString(0);
						 obj.Password = rdr.GetString(1);
						 obj.Nama = rdr.GetString(2);
						 obj.TipeUser = rdr.GetInt16(3);
						 obj.CreatedDate = rdr.GetDateTime(4);
						 obj.CreatedBy = rdr.GetString(5);
						 obj.ModifiedDate = rdr.GetDateTime(6);
						 obj.ModifiedBy = rdr.GetString(7);
						 result.Add(obj);
					  }
			}
			 return result;
		}

	}
}
